/**
 * Created by john on 2016/8/31.
 */
(function () {
    'use strict';

    angular
        .module('user.pay.ways.controller', [])
        .controller('UserPayWaysCtrl',UserPayWaysCtrl);

    UserPayWaysCtrl.$inject = ['$scope','$yikeUtils','$rootScope','$state','$ionicHistory','$ionicModal','$ionicTabsDelegate','localStorageService','$ionicPlatform'];
    /* @ngInject */
    function UserPayWaysCtrl($scope,$yikeUtils,$rootScope,$state,$ionicHistory,$ionicModal,$ionicTabsDelegate,localStorageService,$ionicPlatform) {
        var id=$state.params;
        /*用于遍历单选的数组*/
        $scope.buyType = [];

        $scope.setPayWay = setPayWay;
        $scope.user=localStorageService.get('user');
        // console.log($state.params);
        $scope.money =$state.params.price;
        $scope.confirm = confirm;
        init();
        function init() {
            // $ionicTabsDelegate.showBar(true);//打开导航栏
            $scope.buyType.length = 0;
            $scope.selectPay = "alipay";
            $scope.buyType.push({
                type:"alipay",
                img:"img/recommend/zhifub.png",
                title:"支付宝支付",
                content:"推荐有支付宝账号的用户使用"
            });
            /*if($ionicPlatform.is("IOS")){
                $scope.buyType.length = 0;
                yikeTaishan.innerBuy()
                    .then(function(data){
                        if(data.result.result == 1){
                            $scope.selectPay = "neigou";
                            $scope.buyType.push({
                                type:"neigou",
                                img:"img/recommend/ios.png",
                                title:"内购支付",
                                content:"iPhone用户购买"
                            });
                        }else{
                            $scope.selectPay = "alipay";
                            $scope.buyType.push({
                                type:"alipay",
                                img:"img/recommend/zhifub.png",
                                title:"支付宝支付",
                                content:"推荐有支付宝账号的用户使用"
                            });
                        }
                        $scope.$digest();
                    }).catch(function(err){
                    console.log(err);
                });
            }else{
                $scope.buyType.length = 0;
                $scope.selectPay = "alipay";
                $scope.buyType.push({
                    type:"alipay",
                    img:"img/recommend/zhifub.png",
                    title:"支付宝支付",
                    content:"推荐有支付宝账号的用户使用"
                });
            }*/
        }
        function setPayWay(way){
            $scope.selectPay = way;
        }
        function confirm(way){
            $scope.payway = way;
            if($scope.payway == "wx"){
                window.open('http://114.215.222.89/app/index.php?i=1&c=entry&m=yike_ts_plan&do=pay&op=WeChat_Pay&price='+$scope.money+'&token='+$scope.user.token+'&id='+id.type+'&type=MWEB','_system');
            }else if($scope.payway == "alipay"){
                /*window.open('http://114.215.222.89/app/index.php?i=1&c=entry&m=yike_ts_plan&do=pay&op=WeChat_Pay&price='+$scope.money+'&token='+$scope.user.token+'&id='+id.type+'&type=ALIWAP','_system');*/
                //这个为android的支付宝提交方式
                yikeTaishan.choseChargeWay($scope.payway,id.type,$scope.user.token)
                 .then(function (data) {
                 var subStatus = data.result.set.alipay.alipay_pay;
                 if($ionicPlatform.is("IOS")){
                 if(subStatus){
                 if(data.result.list.type=='1'){
                 try{
                 cordova.plugins.alipay.payment(data.result.list.form,function success(e){
                 console.log(JSON.stringify(e));
                 $yikeUtils.toast('支付成功');
                 yikeTaishan.expire($scope.user.id,$scope.user.token)
                 .then(function (data) {
                 localStorageService.set("user",data.result.user);
                 });
                 },function error(e){
                 console.log(JSON.stringify(e));
                 $yikeUtils.toast('支付失败');
                 });
                 }catch(ex){
                 $yikeUtils.toast('支付未完成');
                 }
                 }
                 }else{
                 $ionicLoading.toast("支付功能已关闭");
                 }
                 }else{
                 if(data.result.list.type=='1'){
                 try{
                 cordova.plugins.alipay.payment(data.result.list.form,function success(e){
                 console.log(JSON.stringify(e));
                 $yikeUtils.toast('支付成功');
                 yikeTaishan.expire($scope.user.id,$scope.user.token)
                 .then(function (data) {
                 localStorageService.set("user",data.result.user);
                 });
                 },function error(e){
                 console.log(JSON.stringify(e));
                 $yikeUtils.toast('支付失败');
                 });
                 }catch(ex){
                 $yikeUtils.toast('支付未完成');
                 }
                 }
                 }
                 });

                //这个为ios的支付宝提交方式
                /*window.cordova.InAppBrowser.open("http://114.215.222.89/app/index.php?i=1&c=entry&m=yike_ts_plan&do=pay&pps=true&op=payment&token="+$scope.user.token+"&id="+id.type+"&type=ALIWAP","_system");*/

            }else{
                inAppPurchase
                    .getProducts(["com.ionicframework.jiucai"])
                    .then(function(products){
                        console.log(products);
                        inAppPurchase
                            .buy(products[0]['productId'])
                            .then(function(data){
                                console.log(data);
                                $yikeUtils.toast(data);
                            })
                            .catch(function(err){
                                console.log(err);
                                $yikeUtils.toast(err);
                            });
                    }).catch(function(err){
                    $yikeUtils.toast(err);
                })
            }
        }
    }
})();
